package com.sdy.hospitalguard.h5.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.DateUtil;
import com.sdy.hospitalguard.biz.model.ApplyPost;
import com.sdy.hospitalguard.biz.service.ApplyPostService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author cgj
 * @since 2020/05/09
 */
@RestController
@RequestMapping("/applyPost")
@Api(tags = "健康打卡接口")
@Slf4j
public class ApplyPostController extends BaseController {
    @Autowired
    private ApplyPostService applyPostService;

    @PostMapping("/saveTodayApplyData")
    @ApiOperation(value = "新增健康打卡数据", response = ApplyPost.class)
    public Response saveTodayApplyData(@RequestBody @ApiParam(value = "applyPost", name = "实体类,传入参数healthAbnormalDetail " +
            "& healthTouchPatient & healthCorridorPatient") ApplyPost applyPost, HttpServletRequest request) throws BizException {
        Integer userId = getUserId(request);
        Assert.isNull(userId, "用户未登录！");
        Boolean aBoolean = applyPostService.insertData(applyPost.setUserId(userId));

        return Response.success(aBoolean);
    }

    @PostMapping("/updateTodayApplyData")
    @ApiOperation(value = "修改健康打卡数据", response = ApplyPost.class)
    public Response updateTodayApplyData(@RequestBody @ApiParam(value = "applyPost", name = "实体类,传入参数id & healthAbnormalDetail " +
            "& healthTouchPatient & healthCorridorPatient") ApplyPost applyPost, HttpServletRequest request) throws BizException {
        Integer userId = getUserId(request);
        Assert.isNull(userId, "用户未登录！");
        Boolean aBoolean = applyPostService.updateData(applyPost.setUserId(userId));
        return Response.success(aBoolean);
    }

    @GetMapping("/getTodayApplyData")
    @ApiOperation(value = "获取本人今日打卡数据", response = ApplyPost.class)
    public Response getTodayApplyData(HttpServletRequest request) throws BizException {
        Integer userId = getUserId(request);
        Assert.isNull(userId, "用户未登录！");
        Date date = new Date();
        String dateDay = DateUtil.getDate(date, "yyyy-MM-dd");
        ApplyPost one = applyPostService.getOne(Wrappers.<ApplyPost>lambdaQuery()
                .eq(ApplyPost::getUserId, userId)
                .likeRight(ApplyPost::getCreateTime, dateDay));
        if (one == null) {
            log.info("今日未健康打卡!");
            return Response.success("今日未打卡!");
        }
        return Response.success(one);
    }

}
